import { defineStore } from 'pinia'
import { emailInfoType, userInfoType, phoneCodeLoginType } from './type'
import { ref } from 'vue'
import { getEmailLoginApi, getPhoneCodeLoginApi } from '../../../api/index'
import API from '../../../api/index'

const { loginApi } = API
export const useInfoStore = defineStore('userInfo', () => {
  const userInfo = ref(JSON.parse(localStorage.getItem('USERINFO') || '{}') as userInfoType)
  const TOKEN = ref(localStorage.getItem('TOKEN') || '')
  const loginQRCodeUrl = ref('')
  const timeId = ref<any>()

  const getEmailLogin = async (emailInfo: emailInfoType) => {
    let cookie = '__csrf=92889a53c939142be2f30221c15bc686; MUSIC_U=0045553851F1685FBAA50F135E2258FE785AFACFD768D58FFFC382F0FDBF8851AA8ED73AC37CE8C12659FC16850127549A1E01FD5DE8341DC6809F82F6B3AE7120F968A375AE0FEA054816F17EF4EA3989F738FCA88FEB0499D893BFC6A624A45E66E35FE306BDB5F132D69FC2C41ECEE87E08D0503C6DD4011B1F7527F50C14D86857D12907C2CEA306FD353039433711099236900B7276FC79F9B7C5360F86FDD096AD49AB5E7153DB88B005ED218721B2432CEC54A3C046058878EF70263D09F628442D942CB4A029F54A328D2FA3B0A06124357D6572DB011DF55F92866C819BE01A8CF0D2D2C6AA894365E9E0E23821F52ED010EEEEE6FC9659A25A2F042ED9AD1803CEEF62E53F7BB8BA1541168B5FE860723F3DF016677907925D3DBBF19601B50EBFBA5ADA11B759FC31A7B9DC; NMTID=00Oxoss8yQY38WM-E7ticXz_2GQ39EAAAGWXZXBeg'

    loginApi.getUserInfoApi({ cookie: cookie }).then((result: any) => {
      userInfo.value = result.data.profile
      TOKEN.value = cookie
      localStorage.setItem('TOKEN', cookie)
      localStorage.setItem('USERID', '396004961')
    })
    return 'ok'
    // const res: any = await getEmailLoginApi(emailInfo)
    // if (res.code == 200) {
    //   userInfo.value = res.profile
    //   TOKEN.value = res.token
    //   return 'ok'
    // } else {
    //   return Promise.reject(new Error(res.message))
    // }
  }
  const getPhoneCodeLogin = async (phoneCodeInfo: phoneCodeLoginType) => {
    const res: any = await getPhoneCodeLoginApi(phoneCodeInfo)
    if (res.code == 200) {
      userInfo.value = res.profile
      TOKEN.value = res.token
      return 'ok'
    } else {
      return Promise.reject(new Error(res.message))
    }
  }
  const closeLogin = () => {
    clearInterval(timeId.value)
  }
  const getQrCodeLogin = async () => {
    // 获取二维码key
    let { data: { unikey } } = await loginApi.getLoginQrCodeKeyApi()
    //     获取二维码图片
    let { data: { qrimg } } = await loginApi.getLoginQrCodeApi(unikey)
    loginQRCodeUrl.value = qrimg
    // 检测二维码是否扫码
    timeId.value = setInterval(() => {
      loginApi.getLoginQrCodeStatusApi({
        key: unikey,
        timestamp: new Date().getTime(),
        qrimg: true
      }).then((res: any) => {
        if (res.code !== 803) return
        // 扫码成功
        loginApi.getUserInfoApi({ cookie: res.cookie }).then((result: any) => {
          userInfo.value = result.data.profile
          TOKEN.value = res.cookie
          localStorage.setItem('TOKEN', TOKEN.value)
          localStorage.setItem('USERID', userInfo.value.userId + '')
          clearInterval(timeId.value)
        })
      })
    }, 1000)
  }
  const logout = () => {
    loginApi.getLogoutApi().then((res: any) => {
      if (res.code === 200) {
        userInfo.value = {} as userInfoType
        TOKEN.value = ''
        localStorage.removeItem('TOKEN')
        localStorage.removeItem('USERID')
        localStorage.removeItem('USERINFO')
      }
    })
  }

  return {
    userInfo,
    TOKEN,
    loginQRCodeUrl,
    closeLogin,
    getEmailLogin,
    getPhoneCodeLogin,
    getQrCodeLogin,
    logout
  }
},
  {
    persist: [
      {
        key: 'TOKEN',
        storage: localStorage,
        // @ts-ignore
        paths: ['TOKEN']
      },
      {
        key: 'USERINFO',
        storage: localStorage,
        // @ts-ignore
        paths: ['userInfo']
      }
    ]
  }
)
